package cn.itcast.item.controller;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;
    /**
     *分页查询商品
     * @return
     */
    @PostMapping("/list")
    public PageDTO<Item>  findGoodsByPage(@RequestBody SearchItemDTO searchItemDTO){
     return   itemService.findGoodsByPage(searchItemDTO);

    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item findGoodsById(@PathVariable String id){
        return itemService.findGoodsById(id);
    }

    /**
     * 添加商品到数据库
     * @param item
     * @return
     */
    @PostMapping
    public ResultDTO addGoods(@RequestBody Item item){
            return  itemService.addGoods(item);
    }

    /**
     * 下架商品
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public ResultDTO shelvesGoodsById( @PathVariable("id") String id,@PathVariable("status") String status){
      return itemService.shelvesGoodsById(id,status);
    }

    /**
     * 修改商品信息
     * @param item
     * @return
     */
    @PutMapping
    public ResultDTO updateGoods(@RequestBody Item item){
        return itemService.updateGoods(item);
    }
    @DeleteMapping("/{id}")
    public ResultDTO deleteGoodsById(@PathVariable String id){
        return itemService.deleteGoodsById(id);
    }
    /**
     * 修改商品库存
     */

    @PutMapping("/stock/{itemId}/{num}")
    public void reductionInventory(@PathVariable("itemId") String itemId,@PathVariable("num") String num){
        itemService.reductionInventory(itemId,num);
    }
    /**
     * 修改商品库存
     */

    @PutMapping("/stock/add/{itemId}/{num}")
    public void addInventory(@PathVariable("itemId") String itemId,@PathVariable("num") String num){
        itemService.addInventory(itemId,num);
    }

}
